/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service.dic;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.boxin.ims.modules.wechat.entity.dic.TimeQuantum;
import com.boxin.ims.modules.wechat.dao.dic.TimeQuantumDao;

/**
 * 时间段Service
 * @author Jakemanse
 * @version 2013-12-16
 */
@Component
@Transactional(readOnly = true)
public class TimeQuantumService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(TimeQuantumService.class);
	
	@Autowired
	private TimeQuantumDao timeQuantumDao;
	
	public TimeQuantum get(Long id) {
		return timeQuantumDao.findOne(id);
	}
	
	public Page<TimeQuantum> find(Page<TimeQuantum> page, TimeQuantum timeQuantum) {
		DetachedCriteria dc = timeQuantumDao.createDetachedCriteria();
		if (timeQuantum.getUser()!=null && timeQuantum.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", timeQuantum.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(timeQuantum.getName())){
			dc.add(Restrictions.like("name", "%"+timeQuantum.getName()+"%"));
		}
		dc.addOrder(Order.desc("id"));
		return timeQuantumDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(TimeQuantum timeQuantum) {
		timeQuantumDao.save(timeQuantum);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		timeQuantumDao.deleteById(id);
	}
	
}
